<?php
/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  News_model extends CI_Model
{
	/**
	 * @var unknown_type
	 */
	var $id  ;  
	/**
	 * @var unknown_type
	 */
	var $category_parent_id  ;  
	/**
	 * @var unknown_type
	 */
	var $name_vi  ; 
	/**
	 * @var unknown_type
	 */
	var $name_en  ;  
	/**
	 * @var unknown_type
	 */
	var $description_vi;
	/**
	 * @var unknown_type
	 */
	var $description_en;
	/**
	 * @var unknown_type
	 */
	var $picture;
	/**
	 * @var unknown_type
	 */
	var $active;
	/*
	 * @access	public
	 */

        function __construct() {
            parent::__construct();
        }

	function News_model()
	{
            parent::__construct();
	}
	
	function create($category){
		
		$sql = "INSERT INTO `gtb_news` " .
				"( `friendly_url`, `id_ncategory`, " .
				"`code_ncategory`, `vi_name`, `en_name`, `vi_summary`, `en_summary`, `vi_description`, " .
				"`en_description`,  " .
				" `active`, `image`, `date_add`, `date_upd`) " .
				"VALUES ";
		
			$sql .= "(" .
					 $this->db->escape($category['friendly_url']).", " .
					 intval($category['id_ncategory']).", " .
					 $this->db->escape($category['code_ncategory']).", " .
					 $this->db->escape($category['vi_name']).", " .
					 $this->db->escape($category['en_name']).", " .
					 $this->db->escape($category['vi_summary']).", " .
					 $this->db->escape($category['en_summary']).", " .
					 "'" . ($category['vi_description'])."', " .
					"'" . ($category['en_description'])."', " .
					 intval($category['active']).", " .
					 $this->db->escape($category['image']).", " .
					 $this->db->escape($category['date_add']).", " .
					 $this->db->escape($category['date_upd'])."" .
					");";
				
		$this->db->query($sql);
		//return $this->db->affected_rows(); 
		return $this->db->insert_id(); 
		
	}
	
	function update($category){
		$sql = "UPDATE gtb_news
				SET 
				friendly_url = " .  $this->db->escape($category['friendly_url']) . ", " .
				"id_ncategory = " .  intval($category['id_ncategory']) . "," .
				"code_ncategory = " .  $this->db->escape($category['code_ncategory']) . ",".
				"vi_name = " .  $this->db->escape($category['vi_name']) . "," .
				"en_name = " .  $this->db->escape($category['en_name']) . ", " .
				"vi_summary = " .  $this->db->escape($category['vi_summary']) . "," .
				"en_summary = " .  $this->db->escape($category['en_summary']) . ", " .
				"vi_description = '" .  ($category['vi_description']) . "', " .
				"en_description = '" .  ($category['en_description']) . "', " .
				"active = " .  intval($category['active']) . ", " .
				"image = " .  $this->db->escape($category['image']) . ", " .
				"date_upd = " .  $this->db->escape($category['date_upd']) .
				" WHERE id_news = " .intval($category['id_news']);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function delete($id){
		if(!is_array($id)) $id = array($id);
		$ids = implode(',', $id);
		$sql = "DELETE FROM gtb_news WHERE id_news IN ($ids)";
		//$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function getAll($offset, $number_record, $category =  -1, $active = -1){
		$result = null;
		$sql = "SELECT n.*, c.vi_name as cat_name  " .
				" FROM gtb_news n " .
				" LEFT JOIN  gtb_ncategory c ON c.id_ncategory = n.id_ncategory" .
				" WHERE  n.id_ncategory = " . intval($category) . " OR -1= " .intval($category) .
				" 		AND n.active = ". intval($active) . " OR -1 = ". intval($active) .
				" ORDER BY n.id_news DESC".
				" LIMIT $offset, $number_record";
		
		$query = $this->db->query($sql);
		$result = $query->result_array();
       	
		return $result;
	}
	
	function countAll($category =  -1,  $active = -1){
		$result = null;	
		$sql = "SELECT COUNT(*) as num_record" .
				" FROM gtb_news " .
				" WHERE  id_ncategory = " . intval($category) . " OR -1= " .intval($category).
				" 		AND active = ". intval($active) . " OR -1 = ". intval($active) ;
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result[0]->num_record;
	}
	
	function getAllByIds($ids){
		$ids = implode(',', $ids);
		$result = null;
		$sql = "SELECT n.* " .
				" FROM gtb_news n " .
				" WHERE id_news IN ($ids) " .
				" ORDER BY n.id_news DESC";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getByParentCode($code_parent){
		$result = null;
		$sql = "SELECT *
				FROM gtb_pcategory WHERE code_parent_pcategory = '".$this->db->escape($code_parent)."' ORDER BY id_pcategory DESC";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getMaxPosition($category_parent_id){
		
		$sql = "SELECT MAX(position) as position_max
				FROM category 
				WHERE category_parent_id = $category_parent_id";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}
       
		return 0;
	}
	
	function getById($id){
		
		$sql = "SELECT *
				FROM gtb_news 
				WHERE id_news = " . intval($id);
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0];
		}
       
		return null;
	}
	
	function getByCode($code){
		
		$sql = "SELECT *
				FROM gtb_product 
				WHERE code = " . $this->db->escape($code);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	function getByFriendlyUrl($link_rewrite){
		
		$sql = "SELECT *
				FROM gtb_news 
				WHERE friendly_url = " . $this->db->escape($link_rewrite);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
function getByFriendlyUrl2($link_rewrite){
		//$sql = "UPDATE gtb_news set friendly_url = CONCAT (id_news, '')";
		//$this->db->query($sql);
		
		$sql = "SELECT *
				FROM gtb_news 
				WHERE friendly_url = " . $this->db->escape($link_rewrite);
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0];
		}
       
		return null;
	}
	
	function isInGroup($user_id, $group_id){
		$sql = "SELECT 1 as is_member FROM user_group ug
				INNER JOIN users u ON u.id = ug.user_id
				WHERE group_id = $group_id AND ug.user_id = $user_id";
		$query = $this->db->query($sql);
		if($query->result())
			return 1;
		
		return 0;
		
	}
	
	function setImage($image, $newsID){
            $sql = "UPDATE gtb_news SET image = ". $this->db->escape($image).
                   " WHERE id_news = " . intval($newsID);
            $query = $this->db->query($sql);
            return $this->db->affected_rows();
        }
	
// for home page:
	function getAllByCategoryIds($ids, $limit = '5', $offset = "0"){
		$ids = implode(',', $ids);
		$result = array();
		$sql = "SELECT n.* " .
				" FROM gtb_news n " .
				" WHERE n.id_ncategory IN ($ids) AND active = 1" .
				" ORDER BY id_news DESC" . 
				" LIMIT " . intval($offset) .", " . intval($limit);
		//echo '<br>' . $sql. '<br>';
		$query = $this->db->query($sql);
		$result = $query->result_array();
		
		return $result;
	}
	
function countAllByCategoryIds($ids){
		$ids = implode(',', $ids);
		$result = array();
		$sql = "SELECT count(*) as num_record " .
				" FROM gtb_news n " .
				" WHERE n.id_ncategory IN ($ids) AND active = 1";

		$query = $this->db->query($sql);
		$result = $query->result_array();
		
		return $result[0]['num_record'];
	}
	
function getNewsOther($news_id, $cat_id, $limit = 10){
		$result = array();
		$sql = "SELECT *
				FROM gtb_news 
				WHERE active = 1 AND id_news < " . intval($news_id) . " AND id_ncategory = " . intval($cat_id).
				" ORDER BY id_news DESC " .
				" LIMIT 0, " . intval($limit);
		$query = $this->db->query($sql);
		
		if($result = $query->result_array()){
			//print_r($result);
			return $result;
		}
       
		return $result;
	}
	
	function search($keyword, $offset = 0, $limit = 20){
		$result = array();
		
		// create fulltext for multy column if search on multy colmn.
		 $sql = "SELECT *, MATCH(vi_name, vi_description) AGAINST (".$this->db->escape($keyword).") AS score
				FROM gtb_news 
				WHERE MATCH (vi_name, vi_description) AGAINST (".$this->db->escape($keyword).")
				ORDER BY id_news DESC";
		 
		$query = $this->db->query($sql);
		
		if($result = $query->result_array()){
			//print_r($result);
			return $result;
		}
       
		return $result;
	}
	
	function countSearch($keyword, $offset = 0, $limit = 20){
		
		// create fulltext for multy column if search on multy colmn.
		 $sql = "SELECT count(*) as number_record
				FROM gtb_news 
				WHERE MATCH (vi_name, vi_description) AGAINST (".$this->db->escape($keyword).")
				";
		 
		$query = $this->db->query($sql);
		
		if($result = $query->result_array()){
			//print_r($result);
			return $result[0]['number_record'];
		}
       
		return 0;
	}
	
//	
}

// EOF

